﻿// var authority = [1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; //用户权限列表
//0: 查看项目 1: 管理项目 2: 查看委托单和委托人 3: 查看、新建（自己的）委托单 4: 新建、查看委托单和委托人
//5: 查看报告 6: 发布报告 7: 制定报告 8: 审核报告 9: 查看通知单 10: 发布通知单（评片） 11: 新建、匹配工艺卡
//12: 审核工艺卡 13: 分配检测小组 14: 分配检测人员 15: 查看派工单 16: 管理检测小组 17: 填写操作记录
//18: 分配权限 19: 检测部门管理

// warning: "authority" 只有19个元素

if (authority[10] === 0) {
    alert("你没有权限查看此内容");
    window.location.href = "index.html";
}

var rawData = [[], []];
var sentMessage;
if (authority[18] == 1 ){
    sentMessage = {
        "role":'superManager',
        "requestType": "now"
    };
} else if(authority[10] == 1){
    sentMessage = {
        "user_part": user.staff_part,
        "role":'Manager',
        "requestType": "now"
    };
}

var postData;
$.ajax({
    url: '/op/photo_list',
    type: 'post',
    data: sentMessage,
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
console.log(postData);
for (let i = 0; i < postData.length; i++) {
    rawData[0].push([
        postData[i].ph_id,
        postData[i].w_pipeNum,
        postData[i].w_weldNum,
        postData[i].w_spec,
        postData[i].w_weldPer,
        postData[i].ph_phNum
    ]);
}
if (authority[18] == 1 ){
    sentMessage = {
        "role":'superManager',
        "requestType": "history"
    };
} else if(authority[10] == 1){
    sentMessage = {
        "user_part": user.staff_part,
        "role":'Manager',
        "requestType": "history"
    };
}
$.ajax({
    url: '/op/photo_list',
    type: 'post',
    data: sentMessage,
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
for (let i = 0; i < postData.length; i++) {
    rawData[1].push([
        postData[i].ph_id,
        postData[i].w_pipeNum,
        postData[i].w_weldNum,
        postData[i].w_spec,
        postData[i].w_weldPer,
        postData[i].ph_phNum,
        postData[i].ph_IQISen,
        postData[i].ph_bmin,
        postData[i].ph_bmax,
        postData[i].ph_quality,
        postData[i].ph_situation,
        postData[i].ph_flaw,
        postData[i].ph_pos,
        postData[i].ph_size,
        postData[i].ph_evalevel,
        postData[i].ph_conclusion,
        postData[i].ph_power
    ]);
}
var dummyData = [].concat(rawData);
var title;
var methodCount;
//显示选项卡
var optionCard = document.getElementById("optionCard");
//初始化表格标题，根据权限类型动态展示新建按钮和选项卡
if (authority[10] === 1) {//检测主管——评片
    optionCard.innerHTML =
        "<li class=\"active\"><a href=\"#lA\" data-toggle=\"tab\">评片</a></li>" +
        "<li><a href=\"#lB\" data-toggle=\"tab\">历史记录</a></li>";
    //document.getElementById("lC").setAttribute("class", "tab-pane");
    document.getElementById("lA").setAttribute("class", "tab-pane active");
    title = [
        ["底片编号", "管线号", "焊缝编号", "规格", "焊工号", "片号", "像质计灵敏度", "底片黑度min", "底片黑度max", "底片质量","检测状态","缺陷性质", "缺陷位置", "缺陷尺寸", "评定等级", "结论", "保存"],//“评片”
        ["底片编号", "管线号", "焊缝编号", "规格", "焊工号", "片号", "像质计灵敏度", "底片黑度min", "底片黑度max", "底片质量","检测状态", "缺陷性质", "缺陷位置", "缺陷尺寸", "评定等级", "结论", "公开"]//“历史记录”
    ];
    methodCount = [6, 16];//除去下拉框的元素数目
}

var pageID = [1, 1, 1];
//设置每一页的行数
var countPerPage=[];

//默认情况下
if(user.countPerPage== 0){
    countPerPage = [10, 10, 10];
    $('.rowNum').val(10)
}else {
    countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
    $('.rowNum').val(user.countPerPage)
}

//点击设置每一页行数的确定按钮
$('.rowsBut').click(function () {
    //获得文本框中输入的值
    var rowsNumbuer = $('.rowNum').val();
    //将该值传入到后台中
    $.ajax({
        url:'/pj/countPerPage',
        type:'post',
        data:{'rowsNumbuer': rowsNumbuer},
        async:false,
        success:function () {
            //console.log('点击确认以后的countPerPage'+user.userId)
            if(user.countPerPage== 0){
                countPerPage = [10, 10, 10];
                $('.rowNum').val(10)
            }else {
                countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
                $('.rowNum').val(user.countPerPage)
            }
        },
        error:function () {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    //刷新页面
    location.reload()
})
var pageButtonCount = [8, 8, 8];


var pageCount;

pageCount = new Array();
for (let i = 0; i < methodCount.length; i++) {
    pageCount[i] = parseInt((rawData[i].length - 1) / countPerPage[1]) + 1;
}

var table = [document.getElementById("table0"), document.getElementById("table1")];
var tbody = [table[0].children[1], table[1].children[1]];
var pages = [document.getElementById("pages0"), document.getElementById("pages1")];
console.log(tbody[0].innerHTML);
//初始化筛选栏可选项
for (let i = 0; i < methodCount.length; i++) {
    for (let j = 0; j < methodCount[i]; j++) {
        let option = document.createElement("option");
        option.innerHTML = title[i][j];
        document.getElementById("searcher" + i).children[1].appendChild(option);
    }
}
//初始化表格
for (let i = 0; i < methodCount.length; i++) {
    let inner = "<td>&nbsp;</td>";
    for (let j = 0; j < title[i].length - 1; j++) {
        inner += "<td />";
    }
    for (let j = 0; j < countPerPage[i]; j++) {
        var tr = document.createElement("tr");
        tr.innerHTML = inner;
        tbody[i].appendChild(tr);
    }
}
//装填标题栏
var evaluatedData = [];
var evaluatedIndex = [];
var publicIndex = [];
for (let i = 0; i < methodCount.length; i++) {
    for (let j = 0; j < (pageCount[i] > pageButtonCount[i] ? pageButtonCount[i] : pageCount[i]); j++) {
        var li = document.createElement("li");
        li.setAttribute("onclick", "OnClick(" + (j + 1) + ", " + i + ")");
        var a = document.createElement("a");
        a.innerHTML = j + 1;
        a.setAttribute("href", "#");
        li.appendChild(a);
        pages[i].insertBefore(li, pages[i].lastElementChild);
    }
    for (let j = 0; j < title[i].length; j++) {
        let t = document.createElement("th");
        t.innerHTML = title[i][j];
        table[i].children[0].children[0].appendChild(t);
    }
    OnClick(1, i);
}

// 生成翻页按钮
// console.log(pages[0].innerHTML);
function OnClick(index, cardIndex) {
    // console.log(cardIndex);
    // console.log("a" + pageID);
    if (index === -1) {
        if (pageID[cardIndex] > 1) OnClick(pageID[cardIndex] - 1, cardIndex);
        return;
    }
    if (index === -2) {
        // console.log("b" + pageID);
        if (pageID[cardIndex] < pageCount[cardIndex]) OnClick(pageID[cardIndex] + 1, cardIndex);
        return;
    }
    if (index === 1 && dummyData[cardIndex].length < countPerPage[cardIndex]) {
        for (let i = 0; i < dummyData[cardIndex].length; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        for (let i = dummyData[cardIndex].length; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "display: none");
        }
        pages[cardIndex].setAttribute("style", "display: none");
    }
    else {
        for (let i = 0; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        pages[cardIndex].setAttribute("style", "");
    }
    pageID[cardIndex] = index;
    // console.log("c" + pageID);
    //显示数据
    for (let i = 0; i < countPerPage[cardIndex]; i++) {
        if (dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i] !== undefined) {
            for (let j = 0; j < methodCount[cardIndex]; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][j];
            }
            //显示操作按钮
            if (authority[10] === 1) {
                //派工单数据——检测主管页面
                if (cardIndex === 0) {
                    /**
                    let input = document.createElement("input");
                    input.setAttribute("size", "5");
                    input.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 12].innerHTML = input.outerHTML;
                    */
                    // let select = document.createElement("select");
                    // select.setAttribute("class", "custom-select d-block w-100");
                    // select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                    //     + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    // select.innerHTML = "<option>8</option>"+"<option>9</option>"+"<option>10</option>"+"<option>11</option>"+"<option>12</option>"+"<option>13</option>"+"<option>14</option>"+"<option>15</option>"+"<option>16</option>";
                    // tbody[cardIndex].children[i].children[title[cardIndex].length - 11].innerHTML = select.outerHTML;
                    //像质计灵敏度
                    let select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>8</option>"+"<option>9</option>"+"<option>10</option>"+"<option>11</option>"+"<option>12</option>"+"<option>13</option>"+"<option>14</option>"+"<option>15</option>"+"<option>16</option>";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 11].innerHTML = select.outerHTML;
                    //底片黑度min
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>2.0</option>";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 10].innerHTML = select.outerHTML;
                    //底片黑度max
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>4.0</option>";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 9].innerHTML = select.outerHTML;
                    //底片质量
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>合格</option>" + "<option>不合格：伪缺陷</option>" + "<option>不合格：灵敏度低</option>" + "<option>不合格：黑度小</option>" + "<option>不合格：黑度大</option>" + "<option>不合格：灰雾度大</option>" + "<option>不合格：标识错误</option>" + "<option>不合格：标识挡焊缝</option>";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 8].innerHTML = select.outerHTML;
                    //检测状态
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>合格</option>" + "<option>底片不合格复拍</option>" + "<option>焊口返修后复拍</option>" + "<option>委托方原因复拍</option>" ;
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 7].innerHTML = select.outerHTML;
                    //缺陷性质
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>圆形缺陷</option>" + "<option>条形缺陷</option>" + "<option>未熔合</option>" + "<option>为焊透</option>" + "<option>裂纹</option>" + "<option>内凹</option>" + "<option>咬边</option>";//缺陷性质
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 6].innerHTML = select.outerHTML;
                    //缺陷位置
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>中心左mm</option>" + "<option>中心右mm</option>" + "<option>左端点右mm</option>"+ "<option>右端点左mm</option>";//缺陷位置
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 5].innerHTML = select.outerHTML;
                    //缺陷尺寸
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>60</option>" + "<option>70</option>" + "<option>80</option>";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 4].innerHTML = select.outerHTML;
                    // ⅠⅡⅢⅣⅤ
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>Ⅰ</option>" + "<option>Ⅱ</option>" + "<option>Ⅲ</option>"+ "<option>IV</option>";//评定等级
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 3].innerHTML = select.outerHTML;
                    //结论
                    select = document.createElement("select");
                    select.setAttribute("class", "custom-select d-block w-100");
                    select.setAttribute("onchange", "OnChange(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    select.innerHTML = "<option>合格</option>" + "<option>不合格</option>";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 2].innerHTML = select.outerHTML;

                    //确认的勾选框
                    let button = document.createElement("input");
                    button.setAttribute("type", "checkbox");
                    button.setAttribute("onclick", "OnConfirm(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;
                    let evaluatedID = evaluatedIndex.indexOf(dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0]);
                    if (evaluatedID >= 0) {
                        for (let j = -9; j > 1; j--) {
                            tbody[cardIndex].children[i].children[title[cardIndex].length + j].children[0].value = evaluatedData[evaluatedID][j + 1];
                        }
                        tbody[cardIndex].children[i].children[title[cardIndex].length - 1].children[0].checked = true;
                    }
                }
                else if (cardIndex === 1) {
                    let button = document.createElement("input");
                    button.setAttribute("type", "checkbox");
                    button.setAttribute("onclick", "OnSetPublic(" + cardIndex + "," + i + ","
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + ", this);");
                    if (dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][14] == 1) {
                        button.setAttribute("disabled", "disabled");
                        button.setAttribute("checked", "checked");
                    }
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;
                }
            }
        } else {
            tbody[cardIndex].children[i].children[0].innerHTML = "&nbsp;";
            for (let j = 1; j < title[cardIndex].length; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = "";
            }
        }
    }
    //刷新翻页按钮
    if (pageCount[cardIndex] > pageButtonCount[cardIndex]) {
        var first = parseInt(pageID[cardIndex] - pageButtonCount[cardIndex] / 2) + 1;
        if (first < 1) first = 1;
        if (first > pageCount[cardIndex] - pageButtonCount[cardIndex] + 1) first = pageCount[cardIndex] - pageButtonCount[cardIndex] + 1;
        for (let i = 0; i < pageButtonCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = first; i < first + pageButtonCount[cardIndex]; i++) {
            var li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + i + ", " + cardIndex + ")");
            if (i === pageID[cardIndex])
                li.setAttribute("class", "active");
            var a = document.createElement("a");
            a.innerHTML = i;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    } else {
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + (i + 1) + ", " + cardIndex + ")");
            if (i + 1 === pageID[cardIndex])
                li.setAttribute("class", "active");
            a = document.createElement("a");
            a.innerHTML = i + 1;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    }
    // console.log("d" + pageID);
}

function OnChange(cardIndex, index, id, element) {
    let temp = [id];
    for (let i = 5; i < 14; i++) {
        temp.push(tbody[cardIndex].children[index].children[i].children[0].value);
    }
    let evaluatedID = evaluatedIndex.indexOf(id);
    if (evaluatedID < 0) {
        evaluatedData.push(temp);
        evaluatedIndex.push(id);
    }
    else {
        evaluatedData[evaluatedID] = temp;
    }
    element.parentNode.parentNode.children[14].children[0].checked = true;
}
function OnConfirm(cardIndex, index, id, element) {
    let temp = [id];
    for (let i = 6; i < 16; i++) {
        temp.push(tbody[cardIndex].children[index].children[i].children[0].value);
    }
    let evaluatedID = evaluatedIndex.indexOf(id);
    if (element.checked) {
        evaluatedData.push(temp);
        evaluatedIndex.push(id);
    }
    else {
        evaluatedData.splice(evaluatedID, 1);
        evaluatedIndex.splice(evaluatedID, 1);
    }
}
function CommitData() {
    var com = [[], [], [], [], [], [], [], [], [], [], []];
    for (let i = 0; i < evaluatedData.length; i++) {
        for (let j = 0; j < evaluatedData[i].length; j++) {
            com[j][i] = evaluatedData[i][j];
        }
    }
    return com;
}
function OnCommit() {
    if (evaluatedData.length == 0) return;
    let com = CommitData();
    let date = new Date();
    $.ajax({
        url: '/op/photo_update',
        type: 'post',
        data: {
            "ph_id": com[0],
            "ph_IQISen": com[1],
            "ph_bmin": com[2],
            "ph_bmax": com[3],
            "ph_quality":com[4],
            "ph_situation":com[5],
            "ph_flaw": com[6],
            "ph_pos": com[7],
            "ph_size": com[8],
            "ph_evalevel": com[9],
            "ph_conclusion": com[10],
            "ph_time": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
            "num": evaluatedData.length
        },
        async: false,
        traditional: true,
        success: function (data, status) {
            if (status == 'success') {
                alert("提交成功");
                location.reload();
            }

        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
}

function OnSetPublic(cardIndex, index, id, element) {
    let publicID = publicIndex.indexOf(id);
    if (element.checked) {
        publicIndex.push(id);
    }
    else {
        publicIndex.splice(publicID, 1);
    }
}

function OnCommitPublic() {
    $.ajax({
        url: '/echo/photo_disclose',
        type: 'post',
        data: {
            "ph_ids": publicIndex
        },
        async: false,
        traditional: true,
        success: function (data, status) {
            if (status == 'success') {
                alert("提交成功");
                location.reload();
            }

        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
}

function OnSearch(cardIndex) {
    
    let searcher = document.getElementById("searcher" + cardIndex);
    let condition = [searcher.children[1].value, searcher.children[3].value];

    var handyData = rawData[cardIndex];//该页面上的信息
    var resultData = new Array();//存放检索结果
    for (let i = 0; i < handyData.length; i++) {
        //字符串匹配，长度为0则全匹配
        var regx0 = new RegExp(condition[0]);// 要检索的管线号
        var regx1 = new RegExp(condition[1]);// 要检索的焊缝号
        if (regx0.exec(handyData[i][1]) && // 管线号字段下标为0 
            regx1.exec(handyData[i][2])) { // 焊缝号字段下标为1

            resultData.push(handyData[i]);//加入符合的项
        }
    }
    dummyData[cardIndex] = resultData;//替换，反正再返回当前页面会变回来

    OnClick(1, cardIndex);
}